<?php
class news extends base{

	public function init(){
		parent::init();

		$this->adObj = D('sys_ad');

		$this->newsObj = D('news');
		$this->news_cateObj = D('news_cate');
		$this->gameObj = D('game');
	}

	public function adlist() 
	{
		$cond = "status=1 and name='news1'";
		$ads = $this->adObj->findAll(array($cond),'sort desc,id asc');
		if($ads){
			foreach($ads as $val){
				$val['img'] = D('upload_file')->get_image_url($val['img']);
				$val['opentype'] = 'navigate';
				$list[] = $val;
			}
		}
		$data['ads'] = $list;
		$this->renderSuccess('获取成功','',$data);
	}

	public function catelist() 
	{
		$cond = "status=1";
		$cates = $this->news_cateObj->findAll(array($cond),'sort desc,id asc');
		$data['cates'] = $cates;
		$this->renderSuccess('获取成功','',$data);
	}

	public function newslist() 
	{
		
		$data = array();
		$cate_id = arg('cate_id');
		$keyword = arg('keyword');
		
		$cond = "status=1 ";
		if($cate_id){
			$cond .=" and cate_id={$cate_id}";
		}
		//搜索
		if($keyword){
			$cond.=" and (title like '%".$keyword."%' or intro like '%".$keyword."%') ";
		}
		
		$result = $this->newsObj->get_datalist($cond,"is_top desc, up_time desc");
		if($result['list']){
			$data['list'] = $result['list'];
			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('数据错误');
		}
		
		
	}

	public function newsinfo()
	{
		$info_id = arg('info_id');
		
		$cond = "id='{$info_id}' and status=1";
		$result = $this->newsObj->get_info($cond);
		if($result){
			$val = $result;
			if($val['imgs']){
				$photos_arr = explode(',', $val['imgs']);
				foreach($photos_arr as $v){
					// $val['imgss'][] = $v;
					$img_url = D('upload_file')->get_image_url($v);
					$val['imgss'][] = $img_url;
					$val['thumbs'][] = $img_url."_thumb.jpg";
				}
				$val['img'] = $val['imgss'][0];
			}
			$val['content'] = __htmlToArray($val['content']);

			$val['cate'] = $this->news_cateObj->get_info($val['cate_id']);
			$val['game'] = $this->gameObj->get_info($val['game_id']);
			$val['game']['logo'] = D('upload_file')->get_image_url($val['game']['logo']);
			$data['info'] = $val;
			
			$this->newsObj->incr(array('id'=>$info_id),'hits');

			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('数据错误');
		}
	}

	public function siteinfo() 
	{
		//区域
		if($this->areas){
			foreach($this->areas as $val){
				$v['label'] = $val['name'];
				$v['value'] = intval($val['id']);
				$areas[] = $v;
			}
		}else{
			$v['label'] = $this->city['name'];
			$v['value'] = $this->city_id;
			$areas[] = $v;
		}
		$data['areas'] = $areas;

		//分类
		foreach($this->cates as $val){
			$v['label'] = $val['name'];
			$v['value'] = intval($val['id']);
			$cates[] = $v;
		}
		$data['cates'] = $cates;

		$site = array(
			'site_name' => $this->setting['site']['site_name'],
		);
		$data['site'] = $site;
		$data['city'] = $this->city;
		$this->renderSuccess('获取成功','',$data);
	}

	

	

	//拨打电话
	public function calltel()
	{
		
		if($this->fmdo=='calltel'){

			$user_id = $this->user['id'];
			$info_id = arg('info_id');
			// $log_id = $this->house_logObj->calltel_log($info_id,$user_id);
			// if($log_id){
			// 	$this->renderSuccess('操作成功','',$log_id);
			// }
			$log_id = 0;
			$this->renderSuccess('操作成功','',$log_id);
		}
	}

	public function yuyue()
	{

		if($this->fmdo == 'add'){

			$formData = $this->formData('formData');
			$yy_day = strtotime($formData['yy_day']);
			$yy_day = date('Y-m-d H:i:s',$yy_day);
			
			$info_id = arg('info_id');
			$cond = "id='{$info_id}' and status=1";
			$result = $this->xinfangObj->get_info($cond);
			if($result){
				$arrBase = array(
					'city_id' => $result['city_id'],
					'area_id' => $result['area_id'],
					'info_id' => $result['id'],
					'user_name' => $formData['user_name'],
					'user_tel' => $formData['user_tel'],
					'yy_day' => $yy_day,
					'add_time' => date('Y-m-d H:i:s'),
					'user_id' => $this->user['id'],
					'status' => 1,
				);
				$house_id = $this->xinfang_yuyueObj->add($arrBase);
				$this->renderSuccess('预约成功');
			}else{
				$this->renderError('数据错误');
			}
			
		}
		
	}

}